<?php
class Admin_CatalogController extends My_Controller_Admin_Action
{
	protected $catalogModel = '';

	protected function _init()
	{
		$this->catalogModel = new Admin_Model_Catalog();
	}

	public function indexAction()
	{
	    $rowPerPage = $this->_getParam('rowperpage', 20);
		$currentPage = 1;
		$page = $this->_getParam('page', 1);
		if(!empty($page)) {
			$currentPage = $page;
		}

		$rows = $this->catalogModel->getListCatalog();
		$paginator = Zend_Paginator::factory($rows);
		$paginator->setItemCountPerPage($rowPerPage);
		$paginator->setCurrentPageNumber($currentPage);

		$this->view->page = $page;
		$this->view->paginator = $paginator;
	}

	public function editCatalogAction()
	{
		$id = $this->_getParam('id');
		$catalogModel = new Admin_Model_Catalog();
		$row = $catalogModel->getCatalog($id);

		$this->view->catalog = $row;
	}

	public function saveCatalogAction() 
	{
	    $catalogModel = new Admin_Model_Catalog();
	    $ok = $this->_getParam('ok');
	    $traite = $this->_getParam('traite');
	    $paye = $this->_getParam('paye');
	    
	    $id = $this->_getParam('id');
	    $catOk="";
    	$count=count($ok);
    	for($i = 0; $i < $count; $i++){
    		$catOk .= $ok[$i]."|";
    	}
    	$catOk = substr($catOk,0,-1);    	
    	$catalogModel->updateCatalog($id, $paye, $catOk, $traite);
    	
    	$this->_redirect('admin/catalog');
	}
	
	public function deleteCatalogAction()
	{
		$id = $this->_getParam('id');
		$page = $this->_getParam('page', 1);
		$catalogModel = new Admin_Model_Catalog();
		$catalogModel->removeCatalog($id);

		$param = '';
		if($page > 1) $param = 'index/page/' . $page;
		$this->_redirect('admin/catalog/' . $param);
		exit;
	}
}

